
exports.upgrade = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.id) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        if( !gConfTech[req.args.id] ) {
            resp.code = 1; resp.desc = 'invalid id'; break;
        }

        var id = +req.args.id;

        var user = player.user;

        var techLevel = (id in user.tech) ? user.tech[id]:0;

        if( techLevel >= user.status.level ) {
            resp.code = 1; resp.desc = 'level up to max'; break;
        }

        var isTenTimes = (+req.args.ten > 0) ? true : false;
        var expectTimes = 1;
        if( isTenTimes ) {
            var diffLevel = user.status.level - techLevel;
            expectTimes = ( diffLevel > 10 ) ? 10 : diffLevel;
        }

        var techConf = gConfTech[id];
        var honorCostSum = 0;
        var isFullLevel = false;
        for( var i = 0; i < expectTimes; i++ ) {
            var newTechLevel = techLevel+1;
            var honorCost = +techConf['Honor'+newTechLevel];
            if( !honorCost ) {
                isFullLevel = true; break;
            }

            if( user.status.honor < honorCostSum + honorCost ) {
                break;    
            }    
            
            techLevel = newTechLevel; 
            honorCostSum += honorCost;
        }

        if( isFullLevel && 0 == i ) {
            resp.code = 1; resp.desc = 'reach to Max'; break; 
        }

        if( !honorCostSum ) {
            resp.code = 1; resp.desc = 'not enough honor'; break; 
        }
        
        player.costResource('honor', honorCostSum);
        user.tech[id] = techLevel;

        player.markDirty('tech.'+id);
        player.roleDirty = true;

        player.doTask('UpgradeTech','all');

        resp.data.level = user.tech[id];
        resp.data.honor = -honorCostSum;

    }while(false);

    onHandled();
}
